# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).

susers = [ # to more dummy users, add more hashes to this array
          {username: 'jrock', firstname: "John"},
          {username: 'carnegie', firstname: 'Dale'},
          {username: 'star_fighter', firstname: 'Timothy'},
          {username: 'gate$', firstname: 'Bill'}
         ]
for i in 0..(susers.length)-1 do
  susers[i] = User.new(susers[i].merge(rank: i+2, active: Time.now - 1000*i, email: "#{susers[i][:username]}@gmail.com"))
end

User.delete_all
gabe = User.new(username: 'gabezilla', firstname: 'Gabe', rank: 1, email: 'gz@whitehouse.gov', active: Time.now)
gabe.password = "123123password"
gabe.save
for u in susers do
  u.password = 'asdf'
  u.save
end

Friend.delete_all
User.all.each do |u|
  if u.username != 'gabezilla' then
    Friend.create(user_id: gabe.id, friend_id: u.id, confirmed: true)
    Friend.create(user_id: u.id, friend_id: gabe.id, confirmed: true)
  end
end

Match.delete_all
UsersMatch.delete_all
wheel = Match.create(owner: gabe.id, title: 'The Strzepek Wheel of Doom', timealloted: 4, startmoney: 500000)
User.all.each do |u|
  um = UsersMatch.new(user_id: u.id, match_id: wheel.id, joined: u.username.length > 5, money: 4500)
  um.stocks = { 
    "MMM" => {"price"=>12713, "change"=>"gain", "shares"=>u.id },
    "AXP" => {"price"=>8212, "change"=>"gain", "shares"=>u.id },
    "T" => {"price"=>3582, "change"=>"gain", "shares"=>u.id }, 
    "BA" => {"price"=>13309, "change"=>"loss", "shares"=>u.id },
    "CAT" => {"price"=>8422, "change"=>"gain", "shares"=>u.id }, 
    "CVX" => {"price"=>12114, "change"=>"gain", "shares"=>u.id },
    "INTC" => {"price"=>2424, "change"=>"gain", "shares"=>u.id }
  }
  um.save
end

raymond = Match.create(owner: gabe.id, title: 'Everybody loves ActiveRecord', timealloted: 4, startmoney: 5000, begintime: Time.now)
User.all.each do |u|
  um = UsersMatch.new(user_id: u.id, match_id: raymond.id, joined: true, money: 2000)
  um.stocks = { 
    "GOOG" => {"price"=>13309, "change"=>"loss", "shares"=>u.id / 3 },
    "CAT" => {"price"=>8422, "change"=>"gain", "shares"=>u.id }, 
    "CVX" => {"price"=>12114, "change"=>"gain", "shares"=>u.id },
    "AAPL" => {"price"=>2424, "change"=>"gain", "shares"=>u.id / 2 }
  }
  um.save
end


old = Match.create(owner: gabe.id, title: 'The oldest game in the world', timealloted: 4, startmoney: 3000, begintime: Time.now - 7.days())
User.all.each do |u|
  um = UsersMatch.new(user_id: u.id, match_id: old.id, joined: true, money: 2000)
  um.stocks = { 
    "GOOG" => {"price"=>13309, "change"=>"loss", "shares"=>u.id / 3 },
    "CAT" => {"price"=>8422, "change"=>"gain", "shares"=>u.id }, 
    "CVX" => {"price"=>12114, "change"=>"gain", "shares"=>u.id },
    "AAPL" => {"price"=>2424, "change"=>"gain", "shares"=>u.id / 2 }
  }
  um.save
end


celebs = ['tbozz']
for celeb in celebs do
  u = User.new(username: celeb, firstname: celeb, email: 'gz@whitehouse.gov', active: Time.now)
  u.password = celeb
  u.save
end

fans = ['steph']
for fan in fans do
  u = User.new(username: fan, firstname: fan, email: 'gz@whitehouse.gov', active: Time.now)
  u.password = fan
  u.save
  Friend.create(user_id: u.id, friend_id: gabe.id, confirmed: false)
end

WatchedStock.delete_all
stocks = ["GOOG", "LCC", "AAMRQ", "CAT", "CVX", "AAPL","BA", "MMM", "AXP", "INTC"]
stocks.each do |stock|
     User.all.each do |user|
          watchedStock = WatchedStock.new("symbol"=>stock, "user_id"=>user.id)
          watchedStock.save
     end
end

UsersMatch.all.each do |u|
     u.updateCapital()
end
